package com.leyou.search.web;

import com.leyou.search.DTO.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@RestController
@RequestMapping("goods")
public class SearchController {

    @Autowired
    private SearchService searchService;


    /**
     * 初始化索引库
     */
    @GetMapping("initialization")
    public ResponseEntity<String> init() {
        searchService.createIndexAndMapping();
        searchService.loadData();
        return ResponseEntity.ok("导入成功");
    }



    /**
     * 查询自动补全
     * @param key 关键字
     * @return 补全内容
     */
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String key){
        return searchService.getSuggestion(key);
    }
    //todo  使用中文输入法时对键盘事件的处理     https://www.cnblogs.com/owenChen/p/3215421.html


    /**
     * 基本搜索
     * @param searchParamDTO 参与搜索的参数
     * @return 商品列表
     */
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoodsList(@RequestBody SearchParamDTO searchParamDTO){

        return searchService.searchGoodsList(searchParamDTO);
    }


}